package games.ninJump.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import games.ninJump.data.GameState;
	
	public class ScoreShow extends Sprite
	{
		private var m_gameState:GameState;
		private var m_bmd:BitmapData;
		private var m_bmdCopy:BitmapData;
		private var m_width:Number;
		private var m_height:Number;
		
		public function ScoreShow(bmd:BitmapData)
		{
			super();
			m_bmd = bmd;
			m_width = m_bmd.width / 10;
			m_height = m_bmd.height;
			scaleX = scaleY = .8;
		}
		
		public function set data(n:GameState):void
		{
			m_gameState = n;
			m_gameState.addEventListener(Event.CHANGE,onChangeState);
		}
		
		private function showScore(n:int):void
		{
		
			var score:String = n.toString();
			var a:Array = score.split("");
			for(var i:int = 0; i < a.length; i++)
			{
				drawNumber(int(a[i]),i * (m_width + 2),0);
			}
			
			x = (GameState.GAME_WIDTH - width) / 2;
			y = 10;
		}
		
		private function drawNumber(num:int,x:Number,y:Number):void
		{
			m_bmdCopy = new BitmapData(m_width,m_height,true,0xFFFFFF);
			m_bmdCopy.copyPixels(m_bmd,new Rectangle(num * m_width,0,m_width,m_height),new Point(0,0))
				
			var bm:Bitmap = new Bitmap(m_bmdCopy);
			bm.smoothing = true;
			addChild(bm);
			bm.x = x;
			bm.y = y;
			
		}
		
		private function clear():void
		{
			while(numChildren > 0)
				removeChildAt(0);
		}
		
		private function onChangeState(e:Event):void
		{
			clear();
			showScore(GameState(e.target).score);
		}
	}
}